﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditSchedule : AdminPageBase
    {
        private ScheduleInfo _scheduleInfo = new ScheduleInfo();
        private readonly ScheduleController _scheduleController = new ScheduleController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["ScheduleId"]))
                _scheduleInfo = _scheduleController.GetSchedule(int.Parse(Request.QueryString["ScheduleId"]));

            if (Page.IsPostBack) return;

            LoadStatus();
            LoadAdminUser();
            LoadLocation();
            LoadScheduleType();

            if (_scheduleInfo.ScheduleId != -1)
                LoadLocationDefault();
                RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListSchedule.aspx?xml=Schedule");
        }

        private void SaveData()
        {
            _scheduleInfo.BeginLocation = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlBeginLocation"]);
            _scheduleInfo.EndLocation = Convert.ToInt32(Request["ctl00$placeHolderContent$ddlEndLocation"]);
            _scheduleInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _scheduleInfo.ScheduleType = int.Parse(ddlScheduleType.SelectedValue);
            if (_scheduleInfo.ScheduleId == -1)
            {
                _scheduleInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _scheduleInfo.CreatedDate = DateTime.Now;
                _scheduleController.InsertSchedule(_scheduleInfo);
            }
            else
            {
                _scheduleInfo.UpdatedBy = UserId;
                _scheduleInfo.UpdatedDate = DateTime.Now;
                _scheduleController.UpdateSchedule(_scheduleInfo);
            }
            Response.Redirect("/Pages/ListSchedule.aspx?xml=Schedule");
        }

        private void RenderForm()
        {
            ddlBeginLocation.SelectedValue = _scheduleInfo.BeginLocation.ToString();
            ddlEndLocation.SelectedValue = _scheduleInfo.EndLocation.ToString();
            ddlStatus.SelectedValue = _scheduleInfo.Status.ToString();
            ddlAdminUser.SelectedValue = _scheduleInfo.CreatedBy.ToString();
            ddlScheduleType.SelectedValue = _scheduleInfo.ScheduleType.ToString();
        }

        private void LoadLocationDefault()
        {
            var locations = new LocationController().ListLocationByScheduleType(_scheduleInfo.ScheduleType);

            ddlBeginLocation.DataSource = locations;
            ddlBeginLocation.DataTextField = "LocationName";
            ddlBeginLocation.DataValueField = "LocationId";
            ddlBeginLocation.DataBind();
            ddlBeginLocation.Items.Insert(0, new ListItem("--- Chọn điểm đi ---", "-1"));

            ddlEndLocation.DataSource = locations;
            ddlEndLocation.DataTextField = "LocationName";
            ddlEndLocation.DataValueField = "LocationId";
            ddlEndLocation.DataBind();
            ddlEndLocation.Items.Insert(0, new ListItem("--- Chọn điểm đến ---", "-1"));
        }

        private void LoadLocation()
        {
            ddlBeginLocation.Items.Insert(0, new ListItem("--- Chọn điểm đi ---", "-1"));
            ddlEndLocation.Items.Insert(0, new ListItem("--- Chọn điểm đến ---", "-1"));
        }

        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }

        public void LoadScheduleType()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/ScheduleType.xml"));
            var dv = ds.Tables["ScheduleType"].DefaultView;
            ddlScheduleType.DataTextField = "Name";
            ddlScheduleType.DataValueField = "ID";
            ddlScheduleType.DataSource = dv;
            ddlScheduleType.DataBind();

            ddlScheduleType.Items.Insert(0,new ListItem("---Chọn loại lịch trình---","-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }

    }
}